package com.qiche.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import com.qiche.aspect.Result;
import com.qiche.dto.ProductCategoryDto;
import com.qiche.mapper.ProductCategoryMapper;
import com.qiche.model.Region;
import com.qiche.dto.ProductItemsDto;
import com.qiche.model.Product;
import com.qiche.service.ProductService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qiche.model.ProductCategory;
import com.qiche.service.ProductCategoryService;

import tk.mybatis.mapper.entity.Example;

/**
 * Created by Linzw on 2018/01/24.
 */
@Service("productCategoryService")
public class ProductCategoryServiceImpl extends BaseService<ProductCategory> implements ProductCategoryService {

	@Autowired
	private ProductCategoryMapper productCategoryMapper;
	@Autowired
	private ProductService productService;
	@Override
	public PageInfo<ProductCategory> selectByPage(ProductCategory category, int page, int limit) {
		//int page = start / length + 1;
		Example example = new Example(ProductCategory.class);
		Example.Criteria criteria = example.createCriteria();
		// 分页查询
		if (category.getName() != null && !"".equals(category.getName())) {
			criteria.andLike("name", "%" + category.getName() + "%");
		}
		PageHelper.startPage(page, limit);
		List<ProductCategory> categoryList = selectByExample(example);
		return new PageInfo<>(categoryList);
	}

	@Override
	public ProductCategory selectById(Long regionId) {
		Example example = new Example(Region.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("id", regionId);
		List<ProductCategory> regionList = selectByExample(example);
		if (regionList.size() > 0) {
			return regionList.get(0);
		}
		return null;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = { Exception.class })
	public Result delCategory(Long categoryId) {

		int deleteCount=mapper.deleteByPrimaryKey(categoryId);
		if(deleteCount==0){
			return Result.createByErrorMessage("删除失败");
		}
		return Result.createBySuccessMessage("删除成功");

	}

	@Override
	public List<ProductCategory> selectAll() {
		// TODO Auto-generated method stub
		return mapper.selectByExample(null);
	}
	@Override
	public List<ProductCategoryDto> selectProductCategoryByMerchantId(Long merchantId){
		List<Product> products = productService.selectByMerchantId(merchantId);
		//通过 CategoryName 分组
		Map<String, List<Product>> groupByCategoryNameList = products.stream()
				.collect(Collectors
						.groupingBy(Product::getCategoryName));
		Set<Map.Entry<String, List<Product>>> entries =
				groupByCategoryNameList.entrySet();

		//服务集合
		List<ProductCategoryDto> productCategoryDtos = new ArrayList<>();


		for (Map.Entry<String, List<Product>> entry : entries) {
			String categoryName = entry.getKey();
			List<Product> productList = entry.getValue();

			ProductCategoryDto productCategoryDto = new ProductCategoryDto();
			productCategoryDtos.add(productCategoryDto);
			productCategoryDto.setCategory(categoryName);
			List<ProductItemsDto> productItemsDtos = productList.stream().map(product -> {
				ProductItemsDto productItemsDto = new ProductItemsDto();
				BeanUtils.copyProperties(product, productItemsDto);
				return productItemsDto;
			}).collect(Collectors.toList());
			productCategoryDto.setProductItems(productItemsDtos);
		}
		return productCategoryDtos;
	}



}
